package com.bike.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bike.entity.HhFood;
import com.bike.entity.HhFoodLib;
import com.bike.entity.HhFoodMenu;
import com.bike.mapper.HhFoodLibMapper;
import com.bike.mapper.HhFoodMapper;
import com.bike.mapper.HhFoodMenuMapper;
import com.bike.util.PageData;
import com.bike.util.TimeManager;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

@Service
public class HhFoodServiceImpl extends BaseServcieImpl  {

	@Autowired
	HhFoodMapper hhFoodMapper;
	@Autowired
	HhFoodMenuMapper hhFoodMenuMapper;
	@Autowired
	HhFoodLibMapper hhFoodLibMapper;
	
	/**
	* 保存
	*/
	public int  saveOrUpdate(PageData pd, HhFood record) {
		if(pd.getLong("foodId")>0){
			 //编辑
			return hhFoodMapper.updateByPrimaryKeySelective(record);
		}else{
			HhFoodMenu foodMenu=hhFoodMenuMapper.selectByPrimaryKey(pd.getLong("foodMenuId"));
			record.setBusinessId(foodMenu.getBusinessId());
			record.setAddTime(TimeManager.currentTime());
			
			HhFoodLib foodLib= hhFoodLibMapper.selectByFoodName(record.getName());
			if(StringUtils.isEmpty(record.getImg())){
				if(foodLib!=null){
					record.setImg(foodLib.getFoodImg());
				}
			}else{
				if(foodLib==null){
					foodLib=new HhFoodLib();
					foodLib.setFoodImg(record.getImg());
					foodLib.setFoodName(record.getName());
					hhFoodLibMapper.insertSelective(foodLib);
				}
			}
			
			return hhFoodMapper.insertSelective(record);
		}
	}
	
	/**
	* 删除
	*/
	public int deleteHhFoodById(long hhFood) {
		HhFood food=new HhFood();
		food.setFoodId(hhFood);
		food.setStatus(1);
		
		return hhFoodMapper.updateByPrimaryKeySelective(food);
	}
	
	/**
	*列表分页
	*/
	public PageData selectHhFoodList(PageData pd) {
		PageData result=new PageData();
		PageBounds pageBounds=(PageBounds) pd.get("pageB");
	 
		List<HhFood> records= hhFoodMapper.selectByPage(pd,pageBounds);
		result.put("rows",records );
		result.put("total", ((PageList<HhFood>) records).getPaginator().getTotalCount());
		return result;
	}
	 
	/**
	*列表(全部)
	*/
	public List<HhFood> listAll(PageData pd) {
		return  hhFoodMapper.selectAll(pd);
	}
	
	/**
	* 通过id获取数据
	*/
	public HhFood selectByHhFoodId(Long hhFood) {
		return hhFoodMapper.selectByPrimaryKey(hhFood);
	}

	public int insertBatch(PageData pd) {
		HhFoodMenu foodMenu=hhFoodMenuMapper.selectByPrimaryKey(pd.getLong("foodMenuId"));
		pd.put("businessId", foodMenu.getBusinessId());
		
		List<List<Object>>  listPd=(List<List<Object>>) pd.get("foods");
		for(List<Object> foodPd:listPd){
			HhFoodLib foodLib=hhFoodLibMapper.selectByFoodName(foodPd.get(0).toString());
			if(foodLib!=null){
				foodPd.add(foodLib.getFoodImg());
			}else{
				foodPd.add("");
			}
		}
		
		return hhFoodMapper.insertBatch(pd);
	}

	public int insertFoodBatch(PageData pd) {
		
		List<List<Object>>  listPd=(List<List<Object>>) pd.get("foods");
//		HhFoodMenu foodMenuBusiness=hhFoodMenuMapper.selectByPrimaryKey(pd.getLong("foodMenuId"));
//		pd.put("businessId", foodMenuBusiness.getBusinessId());
		HhFoodMenu foodMenu=null;
		for(List<Object> foodPd:listPd){
			String foodMenuName=foodPd.get(0).toString();
			if(foodMenu!=null&&foodMenu.getName().equals(foodMenuName)){
				
			}else{
				foodMenu=hhFoodMenuMapper.selectByFoodMenuName(foodMenuName);
				if(foodMenu==null){
					foodMenu=new HhFoodMenu();
					foodMenu.setName(foodMenuName);
					foodMenu.setBusinessId(pd.getLong("businessId"));
					foodMenu.setAddTime(TimeManager.currentTime());
					hhFoodMenuMapper.insertSelective(foodMenu);
				}
				pd.put("foodMenuId", foodMenu.getFoodMenuId());
			}
			
			HhFoodLib foodLib=hhFoodLibMapper.selectByFoodName(foodPd.get(1).toString());
			if(foodLib!=null){
				foodPd.add(foodLib.getFoodImg());
			}else{
				foodPd.add("");
			}
		}
		
		return hhFoodMapper.insertBatch(pd);
	}
}

